
package urnoted;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

import urnoted.Controller;

public class ControlPanel extends JPanel implements ActionListener {
    private static final int WIDTH = 250;
    private static final int HEIGHT = 240;
    private String[] skills = {"Find Key for Note", "Find Note for Key", 
        "Find Key for Tone", "Find Note for Tone"};
    private Controller controller;

    public ControlPanel(Controller controller) {
        this.controller = controller;
        this.setPreferredSize(new Dimension(WIDTH, HEIGHT));
        
        // Skills Panel:
        JPanel skillsPanel = new JPanel();
        skillsPanel.setBorder(BorderFactory.createTitledBorder("Skills"));
        skillsPanel.setLayout(new GridLayout(skills.length, 1));
        JRadioButton[] buttons = new JRadioButton[skills.length];
        ButtonGroup buttonGroup = new ButtonGroup();
        for (int i = 0; i < buttons.length; i++) {
            buttons[i] = new JRadioButton(skills[i]);
            buttonGroup.add(buttons[i]);
            skillsPanel.add(buttons[i]);
            buttons[i].addActionListener(this);
        }
        buttons[0].setSelected(true);
        
        // Songs Panel:
        JPanel songsPanel = new JPanel();
        songsPanel.setBorder(BorderFactory.createTitledBorder("Songs"));
        
        String[] songs = new String[controller.getSongList().size()];
        for(int i = 0;i<controller.getSongList().size();i++)
        {
        	songs[i] = controller.getSongList().get(i).getName();
        }
        
        JComboBox songsBox = new JComboBox(songs);
        songsBox.setMaximumRowCount(4);  // view only 4 songs at a time
        songsBox.addActionListener(this);
        songsPanel.add(songsBox);

        // Start Button:
        JButton startButton = new JButton("Start");
        startButton.addActionListener(this);
        
        add(skillsPanel);
        add(songsPanel);
        add(startButton);
    }

    @Override
    public void actionPerformed(ActionEvent event) {
        String command = event.getActionCommand();
        if (command.equals("Start")) {
            controller.start();
        } else if (command.equals("comboBoxChanged")) {
            JComboBox cb = (JComboBox)event.getSource();
            String song = (String)cb.getSelectedItem();
            controller.setSong(song);
        } else {  // command is from songsBox
            controller.setSkill(command);
        }
    }
}
